package com.wmlive.hhvideo.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatEditText;
import android.util.AttributeSet;

import cn.wmlive.hhvideo.R;

/**
 * Created by lsq on 5/31/2017.
 */

public class DrawableEditText extends AppCompatEditText {

    public static final int LEFT = 1;
    public static final int TOP = 2;
    public static final int RIGHT = 3;
    public static final int BOTTOM = 4;

    private int mHeight;
    private int mWidth;
    private Drawable mDrawable;

    private int mLocation;

    public DrawableEditText(Context context) {
        this(context, null);
    }

    public DrawableEditText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawableEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DrawableTextView);
        try {
            mWidth = a.getDimensionPixelSize(R.styleable.DrawableTextView_drawableWidth, 0);
            mHeight = a.getDimensionPixelSize(R.styleable.DrawableTextView_drawableHeight, 0);
            mDrawable = a.getDrawable(R.styleable.DrawableTextView_drawableSrc);
            mLocation = a.getInt(R.styleable.DrawableTextView_drawableLocation, LEFT);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            a.recycle();
        }
        drawDrawable();
    }

    /*移除图片*/
    public DrawableEditText removeDrawable() {
        this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        return this;
    }

    /**
     * 绘制Drawable宽高,位置
     */
    private void drawDrawable() {
        if (mDrawable != null) {
            Bitmap bitmap = ((BitmapDrawable) mDrawable).getBitmap();
            Drawable drawable;
            if (mWidth != 0 && mHeight != 0) {
                drawable = new BitmapDrawable(getResources(), getBitmap(bitmap, mWidth, mHeight));

            } else {
                drawable = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true));
            }
            switch (mLocation) {
                case LEFT:
                    this.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
                    break;
                case TOP:
                    this.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
                    break;
                case RIGHT:
                    this.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
                    break;
                case BOTTOM:
                    this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, drawable);
                    break;
            }
        }
    }

    private Bitmap getBitmap(Bitmap bm, int newWidth, int newHeight) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = (float) newWidth / width;
        float scaleHeight = (float) newHeight / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
    }
}
